<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Sort;
use App\Models\Tag;
use App\Http\Requests\SortCreateRequest;
use App\Http\Requests\SortUpdateRequest;

class SortController extends Controller
{
    protected $fields = [
        'name' => '',
        'parent_id' => 0,
        'created_at' => '',
        'updated_at' => '',
        'deleted_at' => ''
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Sort::all();
        // $data = Sort::name('ggg')->get();
        // $sorts = Sort::find(2)->posts;
        // $sort = $sort->toArray();
        // foreach($sorts as $sort){
        //     echo $sort->title;
        //     echo '<br>';
        // }die;
        return view('backend.sort.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        
        foreach($this->fields as $field => $default){
            $data[$field] = old($field, $default);
        }
        $data['allSorts'] = Sort::all();

        return view('backend.sort.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SortCreateRequest $request)
    {
        $sort = new Sort();
        $sort = $sort->fill($request->toArray())->save();
        Session::set('_new-sort', trans('messages.create_success', ['entity' => 'Sort']));
        return redirect('/admin/sort');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $sort = Sort::findOrFail($id);
        $data = ['id'=>$id];
        foreach(array_keys($this->fields) as $field){
            $data[$field] = old($field, $sort->$field);
        }

        $data['allSorts'] = Sort::all();

        return view('backend.sort.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SortUpdateRequest $request, $id)
    {
        $sort = Sort::findOrFail($id);
        $sort->fill($request->toArray())->save();
        $sort->save();
        Session::set('_update-sort', trans('messages.update_success', ['entity' => 'Sort']));
        return redirect("/admin/sort");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $sort = Sort::findOrFail($id);
        $sort->delete();
        Session::set('_delete-sort', trans('messages.delete_success', ['entity' => 'Sort']));

        return redirect('/admin/sort');
    }

    /**
     * 软删除一个分类
     *
     * @param null
     * @return null
     */
    public function del() {
        $sort = Sort::find(1);
        $sort->forceDelete();
        if($sort->trashed()){
            echo "成功";
        }else{
            echo "失败";
        }
    }

    /**
     * 查询所有记录，包括软删除
     */
    public function fight() {
        $sorts = Sort::onlyTrashed()->get();
        foreach($sorts as $sort){
            echo $sort->name . '<br>';
        }
    }
}
